package dnuifood_cjy.dnuifood_16110100529.controller;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Switch;
import android.widget.Toast;

import dnuifood_cjy.dnuifood_16110100529.MainActivity;
import dnuifood_cjy.dnuifood_16110100529.R;
import dnuifood_cjy.dnuifood_16110100529.bean.UserBean;
import dnuifood_cjy.dnuifood_16110100529.listener.OnRetrofitListener;
import dnuifood_cjy.dnuifood_16110100529.model.UserModel;

public class LoginActivity extends BaseActivity
        implements OnRetrofitListener<UserBean> {
    private EditText username;
    private EditText userpass;
    private Button btn_login;
    private Button btn_reg;
    private SharedPreferences sp;
    private Switch sw;
    private String sessionID = "";

    Bundle bundle = new Bundle();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //setContentView(R.layout.activity_login);
        initViews();
        initEvents();
        sp=getSharedPreferences("login",MODE_PRIVATE);
        readSP();
    }
    //读取sp信息
    private void readSP() {

        String strname=sp.getString("username",null);
        String strpass=sp.getString("user_id",null);
        Boolean remember=sp.getBoolean("remember",false);
        if (remember)
        {
            username.setText(strname);
            userpass.setText(strpass);
        }
        sw.setChecked(remember);
    }
    public  void  saveUser(String user_id){
        SharedPreferences sharedPreferences = getSharedPreferences("userInfo", Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPreferences.edit();//获取编辑器
        editor.putString("username",username.getText().toString());
        editor.putString("userid",new UserBean().getUserid());
        //int userid = Integer.parseInt(user_id);
        editor.putString("user_id",user_id);
        editor.putString("userpass",userpass.getText().toString());
        editor.commit();//提交
    }

     void initViews() {
        setLayout(R.layout.activity_login);
        btn_reg = findViewById(R.id.btn_regist);
        btn_login = findViewById(R.id.btn_login);
        username = findViewById(R.id.username);
        userpass = findViewById(R.id.password);
         sw=findViewById(R.id.switch1);
    }
    void initEvents() {
        btn_login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(username.getText().toString().trim().equals("")||userpass.getText().toString().trim().equals(""))
                {
                    Toast.makeText(LoginActivity.this,"用户名和密码不能为空",Toast.LENGTH_SHORT).show();
                }
                else
                {
                    login();
//                    username.setText(getIntent().getStringExtra("username"));
//
//                    userpass.setText(getIntent().getStringExtra("password"));

                }
            }
        });
        btn_reg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                bundle.putString ("data", "test");
                Intent intent = new Intent(LoginActivity.this,RegisterActivity.class);
                startActivityForResult(intent,1);//在登录界面启动注册界面
            }
        });
    }
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        String et_user = data.getStringExtra("user");
        username.setText(et_user);
    }//在登录界面(LoginActivity)接收registerActivity中传来的参数，并显示在 EditText 中。


    @Override
    void initData() {

    }

    public void login() {
        UserModel userModel = new UserModel();
        userModel.login(username.getText().toString().trim(), userpass.getText().toString().trim(), this);
    }


    @Override
    public void onSuccess(UserBean userid,int flag) {
        if (userid.getUserid().equals("0"))
        {
            Toast.makeText(LoginActivity.this,"登录失败",Toast.LENGTH_SHORT).show();
        }
        else
        {
            saveUser(userid.getUserid());
            Intent intent = new Intent(LoginActivity.this,MainActivity.class);
            startActivity(intent);
            Toast.makeText(LoginActivity.this,"登录成功",Toast.LENGTH_SHORT).show();
//            LoginActivity.this.finish();
        }
    }//77

    @Override
    public void onFailure(String string) {
        Toast.makeText(LoginActivity.this, string,Toast.LENGTH_SHORT).show();
    }

}